package com.qf.hotel.dao.impl;

import com.qf.hotel.dao.FoodTypeDao;
import com.qf.hotel.pojo.FoodType;
import com.qf.hotel.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author: GsheN丶
 * @date: 2021/6/24 19:35
 * @description:
 */
public class FoodTypeDaoImpl implements FoodTypeDao {
    QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());


    /**
     *
     * @author: GsheN丶
     * @date: 2021/6/24 19:36
     * @description:通过条件查询
     */
    @Override
    public List<FoodType> findByCondition(FoodType foodType) throws SQLException {



        String sql = "SELECT cuisine_id AS typeId, cuisine_name AS typeName FROM t_cuisine WHERE cuisine_name LIKE ?";
        return queryRunner.query(JdbcUtils.getConnection(),sql,new BeanListHandler<>(FoodType.class),foodType.getTypeName());

    }


    /**
     *
     * @author: GsheN丶
     * @date: 2021/6/24 22:05
     * @description:通过ID查询
     */
    @Override
    public FoodType findById(int id) throws SQLException {
        String sql = "SELECT cuisine_id AS typeId, cuisine_name AS typeName FROM t_cuisine WHERE cuisine_id = ?";
        return queryRunner.query(JdbcUtils.getConnection(),sql,new BeanHandler<>(FoodType.class),id);

    }


    /**
     *
     * @author: GsheN丶
     * @date: 2021/6/24 22:05
     * @description:修改
     */
    @Override
    public int update(FoodType foodType) throws SQLException {
        String sql = "UPDATE t_cuisine SET cuisine_name = ? WHERE cuisine_id = ?";
        return queryRunner.update(JdbcUtils.getConnection(),sql,foodType.getTypeName(),foodType.getTypeId());
    }
}
